<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #div1 {
            width: 400px;
            height: 300px;
            margin: 10px auto;
            border: 1px solid red;
        }

        #info {
            width: 400px;
            height: 100px;
            margin: 10px auto;
        }
    </style>
</head>

<body>
    <div id="div1"></div>
    <div id="info">
        <h1>x:<span>0</span>;y:<span>0</span></h1>
    </div>

    <script>
        //当鼠标在div1中移动时，显示坐标值
        var div1 = document.querySelector('#div1');

        //只在div1盒子内部生效
        // div1.addEventListener('mousemove', function (e) {
        //     // e==e||window.event
        //     //获取info中的span标签
        //     console.log(e.target);
        //     var spans = document.querySelectorAll('#info span');
        //     spans[0].innerHTML = e.pageX;
        //     spans[1].innerHTML = e.pageY;
        // }, false);

        //在整个浏览器页面生效
        div1.parentNode.addEventListener('mousemove', function (e) {
            // e==e||window.event
            //获取info中的span标签
            console.log('target:' + e.target);
            console.log('this:' + this);
            var spans = document.querySelectorAll('#info span');
            spans[0].innerHTML = e.pageX;
            spans[1].innerHTML = e.pageY;
        }, false);
    </script>
</body>

</html>